package com.telit.common.util;

import cn.hutool.core.util.StrUtil;
import com.telit.common.core.utils.Result;
import com.telit.common.vo.FileVO;
import com.telit.global.oss.api.feign.RemoteOssService;
import com.telit.global.oss.api.model.UploadFileModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Create by Chencf 2023/01/17
 */
@Slf4j
@Component
public class OssUtils {


    private static RemoteOssService remoteOssService;

    @Autowired
    public OssUtils(RemoteOssService remoteOssService) {
        OssUtils.remoteOssService = remoteOssService;
    }

    public static void setRemoteOssService(RemoteOssService remoteOssService) {
        OssUtils.remoteOssService = remoteOssService;
    }

    public static FileVO upload(MultipartFile multipartFile, String bucket, String filePrefixName) {
       // File file = null;
        try {
            FileVO fileVO = new FileVO();
//            FileInputStream fileInputStream;
//            MultipartFile[] multipartFiles = new MultipartFile[1];
//            String fileName = multipartFile.getOriginalFilename();
//            //String mime = MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM).toString();
//            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
//            String[] filename = fileName.split("\\.");
//            file = File.createTempFile(filename[0], filename[1]);
//            multipartFile.transferTo(file);
//            file.deleteOnExit();
//            fileInputStream = new FileInputStream(file);
//            multipartFiles[0] = new MockMultipartFile(fileName, fileName, suffix, fileInputStream);
            Result<UploadFileModel> result = remoteOssService.upload(bucket, multipartFile);
            fileVO.setFilePath(result.getData().getFileUrl());
            fileVO.setOriginalFilename(result.getData().getFileName());
            return fileVO;
        } catch (Exception e) {
            log.error("文件上传异常操作异常", e);
        }
        return null;
    }

    public static  List<FileVO> uploads(List<MultipartFile> filesList, String bucket) {
        List<FileVO> result = new ArrayList<>();
        MultipartFile[] multipartFiles = filesList.toArray(filesList.toArray(new MultipartFile[filesList.size()]));
        Result<List<UploadFileModel>> uploads = remoteOssService.uploads(bucket, multipartFiles);
        List<UploadFileModel> data = uploads.getData();
        for (UploadFileModel uploadFileModel : data) {
            FileVO fileVO = new FileVO();
            fileVO.setFilePath(uploadFileModel.getFileUrl());
            fileVO.setOriginalFilename(uploadFileModel.getFileName());
            result.add(fileVO);
        }
//        List<String> collect = data.stream().map(uploadFileModel -> uploadFileModel.getFileUrl()).collect(Collectors.toList());
//        return collect.toString().substring(1, collect.toString().length() - 1);
        return result;
    }

    /**
     * 通过文件流上传到minio
     *
     * @param name             文件名
     * @param originalFilename 文件名
     * @param contentType      二进制流内容格式
     * @param is               二进制流
     * @param bucket           桶名称
     *
     * @return 文件路径
     */
    public static String upload(String name, String originalFilename, String contentType, InputStream is,
                                String bucket) {
        if (StrUtil.isEmpty(contentType)) {
            contentType = MediaType.APPLICATION_OCTET_STREAM.toString();
        }
        if (StrUtil.isEmpty(bucket)) {
            bucket = "public";
        }
        MultipartFile[] multipartFiles = new MultipartFile[1];
        try {
            multipartFiles[0] = new MockMultipartFile(name, originalFilename, contentType, is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Result<List<UploadFileModel>> result = remoteOssService.uploads(bucket, multipartFiles);
        return result.getData().get(0).getFileUrl();
    }

}
